package cn.com.api.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiT_member_partner_task;
import cn.com.api.entity.ApiT_member_task_process;
import cn.com.api.mapper.ApiT_member_entrepreneurMapper;
import cn.com.api.mapper.ApiT_member_partnerMapper;
import cn.com.api.mapper.ApiT_member_task_processMapper;

import cn.com.api.service.ApiT_member_task_processService;
import cn.com.api.service.impl.base.T_member_task_processServiceImplBase;
import cn.com.util.DateUtil;
import cn.com.util.PageInfo;

/**
 * 
 **/
@Service
public class ApiT_member_task_processServiceImpl extends T_member_task_processServiceImplBase
		implements ApiT_member_task_processService {
	@Autowired
	private ApiT_member_task_processMapper apiT_member_task_processMapper;
	@Autowired
	private ApiT_member_partnerMapper apiT_member_partnerMapper;
	@Autowired
	private ApiT_member_entrepreneurMapper apiT_member_entrepreneurMapper;

	// 新增对象后返回记录ID
	public int T_member_task_processInsertion(ApiT_member_task_process t_member_task_process) {
		return this.apiT_member_task_processMapper.T_member_task_processInsertion(t_member_task_process);
	}

	// 更新用户任务完成进度
	public void updateTaskProcesss() {
		Map map = new HashMap();
		map.put("task_is_finish", 0); // 只跟新未完成的任务
		List<ApiT_member_task_process> list = apiT_member_task_processMapper.findMemberTaskProcesssByMap(map);
		for (ApiT_member_task_process apiT_member_task_process : list) {
			// 先判断是否任务是否超时
			Date now_time = DateUtil.nowFormatDate("yyyy-MM-dd HH:00:00");
			Date dead_time = DateUtil.stringToDate(apiT_member_task_process.getDead_time(), "yyyy-MM-dd HH:00:00");
			if (dead_time.before(now_time)) {// 任务超时
				apiT_member_task_process.setTask_is_finish(2);
				apiT_member_task_processMapper.updateT_member_task_process(apiT_member_task_process);
			} else {// 更新数据
				String user_id = apiT_member_task_process.getUser_id();

				// 用户邀请合伙人的数量（对应普通核伙人个数）
				Integer partnercount = apiT_member_partnerMapper.getNormalPartnerCount(user_id);
				apiT_member_task_process.setCurr_partner_count(partnercount);

				// 用户邀请成为企业主
				Integer entrepreneur = apiT_member_entrepreneurMapper.getInviteEntrepreneurCount(user_id);
				apiT_member_task_process.setCurr_ent_count(entrepreneur);

				// 更新广告金额

				// 判断任务是否完成
				ApiT_member_partner_task apiT_member_partner_task = apiT_member_task_process
						.getApiT_member_partner_task();
				if (apiT_member_partner_task.getInvite_entrepreneur_amount() <= entrepreneur
						&& apiT_member_partner_task.getInvite_partner_amount() <= partnercount) {// 完成
					apiT_member_task_process.setTask_is_finish(1);
				}
				apiT_member_task_processMapper.updateT_member_task_process(apiT_member_task_process);
			}

		}
	}

}
